// 模拟数据库数据
let users = [
  {
    id: 1,
    username: 'admin',
    password: 'admin123', // 实际项目中应该加密存储
    email: 'admin@example.com',
    phone: '13800138000',
    status: 'active',
    role: 'admin',
    permissions: ['user:read', 'user:write', 'role:read', 'role:write', 'department:read', 'department:write'],
    createTime: '2023-01-01 00:00:00'
  },
  {
    id: 2,
    username: 'user1',
    password: 'user123',
    email: 'user1@example.com',
    phone: '13800138001',
    status: 'active',
    role: 'user',
    permissions: ['user:read'],
    createTime: '2023-01-02 00:00:00'
  }
];

let roles = [
  {
    id: 1,
    name: '管理员',
    code: 'admin',
    description: '系统管理员，拥有所有权限',
    permissions: ['user:read', 'user:write', 'role:read', 'role:write', 'department:read', 'department:write'],
    createTime: '2023-01-01 00:00:00'
  },
  {
    id: 2,
    name: '普通用户',
    code: 'user',
    description: '普通用户，只有基本权限',
    permissions: ['user:read'],
    createTime: '2023-01-02 00:00:00'
  }
];

let departments = [
  {
    id: 1,
    name: '技术部',
    code: 'tech',
    parentId: null,
    leader: '张三',
    description: '负责技术开发',
    createTime: '2023-01-01 00:00:00'
  },
  {
    id: 2,
    name: '产品部',
    code: 'product',
    parentId: null,
    leader: '李四',
    description: '负责产品设计',
    createTime: '2023-01-02 00:00:00'
  }
];

// 用户相关操作
const userModel = {
  findAll: () => users,
  findById: (id) => users.find(user => user.id === parseInt(id)),
  findByUsername: (username) => users.find(user => user.username === username),
  create: (userData) => {
    const newUser = {
      id: users.length + 1,
      ...userData,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
    };
    users.push(newUser);
    return newUser;
  },
  update: (id, userData) => {
    const index = users.findIndex(user => user.id === parseInt(id));
    if (index !== -1) {
      users[index] = { ...users[index], ...userData };
      return users[index];
    }
    return null;
  },
  delete: (id) => {
    const index = users.findIndex(user => user.id === parseInt(id));
    if (index !== -1) {
      const deletedUser = users.splice(index, 1)[0];
      return deletedUser;
    }
    return null;
  }
};

// 角色相关操作
const roleModel = {
  findAll: () => roles,
  findById: (id) => roles.find(role => role.id === parseInt(id)),
  create: (roleData) => {
    const newRole = {
      id: roles.length + 1,
      ...roleData,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
    };
    roles.push(newRole);
    return newRole;
  },
  update: (id, roleData) => {
    const index = roles.findIndex(role => role.id === parseInt(id));
    if (index !== -1) {
      roles[index] = { ...roles[index], ...roleData };
      return roles[index];
    }
    return null;
  },
  delete: (id) => {
    const index = roles.findIndex(role => role.id === parseInt(id));
    if (index !== -1) {
      const deletedRole = roles.splice(index, 1)[0];
      return deletedRole;
    }
    return null;
  }
};

// 部门相关操作
const departmentModel = {
  findAll: () => departments,
  findById: (id) => departments.find(dept => dept.id === parseInt(id)),
  create: (deptData) => {
    const newDept = {
      id: departments.length + 1,
      ...deptData,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
    };
    departments.push(newDept);
    return newDept;
  },
  update: (id, deptData) => {
    const index = departments.findIndex(dept => dept.id === parseInt(id));
    if (index !== -1) {
      departments[index] = { ...departments[index], ...deptData };
      return departments[index];
    }
    return null;
  },
  delete: (id) => {
    const index = departments.findIndex(dept => dept.id === parseInt(id));
    if (index !== -1) {
      const deletedDept = departments.splice(index, 1)[0];
      return deletedDept;
    }
    return null;
  }
};

module.exports = {
  userModel,
  roleModel,
  departmentModel
}; 